
import { _decorator, Component, Node, RigidBody, Vec3, Collider } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Ball')
export class Ball extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    // 初始速度
    private initSpeed = 0


    start() {
        // [3]

        // 获取当前控件的碰撞体
        let coll = this.getComponent(Collider);
        // 存在就监听
        if (coll) {
            coll.on('onCollisionEnter', this.onColl, this);
        }

    }

    // 碰撞函数。用来让球的速度保持初始值，防止碰撞之后速度累加
    onColl() {
        console.log('碰撞到跳板')
        // 当前控件存在刚体，就设置速度
        let rBody = this.node.getComponent(RigidBody);
        if (rBody)
        // 初始速度为0就获取当前默认速度，将其设置为初始速度
            if (this.initSpeed == 0) {
                let vc = new Vec3()
                rBody?.getLinearVelocity(vc);
                this.initSpeed = vc.y;
                this.initSpeed = 4;

            } else {
                // 设置控件速度为初始速度
                rBody?.setLinearVelocity(new Vec3(0, this.initSpeed, 0));
            }
    }


    boost() {

        // 获取当前控件的刚体
        let rBody = this.node.getComponent(RigidBody);
        // 存在刚体就设置移动速度
        if (rBody) {
            rBody.setLinearVelocity(new Vec3(0, -4, 0));
        }

    }


    // update (deltaTime: number) {
    //     // [4]
    // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
